{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "37e922b1-b9dc-4a7a-a304-87d796b165ca",
   "metadata": {},
   "source": [
    "# 字符串"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "2769fc0a-16e9-4487-9d92-33f133247555",
   "metadata": {
    "tags": []
   },
   "source": [
    "由零个或多个字符组成。   顾名思义 字符串"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 18,
   "id": "ccb44347-9cc0-4a95-9889-9cc4cac9f5e6",
   "metadata": {
    "tags": []
   },
   "outputs": [],
   "source": [
    "var1 = \"qwert\"\n",
    "var2 = \"1234\""
   ]
  },
  {
   "cell_type": "markdown",
   "id": "8beec0f5-d217-4ab6-abac-11f25ae1eb72",
   "metadata": {},
   "source": [
    "![拾光梦禧](https://gitee.com/lmq886/image-warehouse/raw/master/img/拾光梦禧.png)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 28,
   "id": "c6954bbd-5c6b-491f-bc6b-dd001693893d",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "str"
      ]
     },
     "execution_count": 28,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "type(var1)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 52,
   "id": "2cf740f9-0e15-4b90-8da4-90be9dbb2144",
   "metadata": {
    "tags": []
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "5"
      ]
     },
     "execution_count": 52,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "len(var1)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 30,
   "id": "00460c20-4db0-40fc-b227-e26db7dfad15",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'q'"
      ]
     },
     "execution_count": 30,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "var1[0]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 34,
   "id": "16930108-0a98-43cd-9534-1bd96a22b89b",
   "metadata": {},
   "outputs": [
    {
     "ename": "IndexError",
     "evalue": "string index out of range",
     "output_type": "error",
     "traceback": [
      "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m",
      "\u001b[1;31mIndexError\u001b[0m                                Traceback (most recent call last)",
      "Cell \u001b[1;32mIn[34], line 1\u001b[0m\n\u001b[1;32m----> 1\u001b[0m var1[\u001b[38;5;241m5\u001b[39m]\n",
      "\u001b[1;31mIndexError\u001b[0m: string index out of range"
     ]
    }
   ],
   "source": [
    "var1[5]"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "be995c79-ceb6-433f-8547-24d6395d2b14",
   "metadata": {},
   "source": [
    "字符串运算"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "48a77d8c-b671-4a93-b823-025e5fe76629",
   "metadata": {},
   "source": [
    "| 操作符 | 描述                                                         | 实例                            |\n",
    "| :----- | :----------------------------------------------------------- | :------------------------------ |\n",
    "| +      | 字符串连接                                                   | a + b 输出结果： HelloPython    |\n",
    "| *      | 重复输出字符串                                               | a*2 输出结果：HelloHello        |\n",
    "| []     | 通过索引获取字符串中字符                                     | a[1] 输出结果 **e**             |\n",
    "| [ : ]  | 截取字符串中的一部分，遵循**左闭右开**原则，str[0:2] 是不包含第 3 个字符的。 | a[1:4] 输出结果 **ell**         |\n",
    "| in     | 成员运算符 - 如果字符串中包含给定的字符返回 True             | **'H' in a** 输出结果 True      |\n",
    "| not in | 成员运算符 - 如果字符串中不包含给定的字符返回 True           | **'M' not in a** 输出结果 True  |\n",
    "| r/R    | 原始字符串 - 原始字符串：所有的字符串都是直接按照字面的意思来使用，没有转义特殊或不能打印的字符。 原始字符串除在字符串的第一个引号前加上字母 **r**（可以大小写）以外，与普通字符串有着几乎完全相同的语法。 | `print( r'\\n' ) print( R'\\n' )` |\n",
    "| %      | 格式字符串                                                   | 请看下一节内容。                |"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 75,
   "id": "9b1ad254-4801-400f-9345-b8503f6286a4",
   "metadata": {
    "tags": []
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'qwert1234'"
      ]
     },
     "execution_count": 75,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "var1 + var2"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 83,
   "id": "cf715d10-e8b8-48cd-82fa-3da1f0335d38",
   "metadata": {
    "tags": []
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'= = = = = '"
      ]
     },
     "execution_count": 83,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "\"= \"*5"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 93,
   "id": "3f3a00d4-edf1-47f6-b258-1dd4f248314f",
   "metadata": {
    "tags": []
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'w'"
      ]
     },
     "execution_count": 93,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "var1[1]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 91,
   "id": "9d13790a-a827-40b2-b83c-dfd66f4489e1",
   "metadata": {
    "tags": []
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'we'"
      ]
     },
     "execution_count": 91,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "var1[1:3]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 99,
   "id": "d11db588-4e50-44e4-95d2-0c8514532ebb",
   "metadata": {
    "tags": []
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "False"
      ]
     },
     "execution_count": 99,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "\"world\" in \"w\""
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 101,
   "id": "3b3992ea-f037-4897-ad96-3fb1434bde2e",
   "metadata": {
    "tags": []
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "True"
      ]
     },
     "execution_count": 101,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "\"w\" in \"world\""
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 103,
   "id": "6fa4432a-7bc6-402c-8ecd-54aa74063256",
   "metadata": {
    "tags": []
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "False"
      ]
     },
     "execution_count": 103,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "\"w\" not in \"world\""
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 40,
   "id": "75f4ce37-78e0-49d9-a47b-90e673643539",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Hello \t World!\n"
     ]
    }
   ],
   "source": [
    "print(\"Hello \\t World!\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 42,
   "id": "6cce640c-4fbd-4aea-8153-6cde9bba78cf",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Hello \u000b",
      " World!\n"
     ]
    }
   ],
   "source": [
    "print(\"Hello \\v World!\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 44,
   "id": "d362257d-fc4a-4e84-a263-45c560233dda",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'\\\\n'"
      ]
     },
     "execution_count": 44,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "r\"\\n\""
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 60,
   "id": "39d3ccdb-520b-4d33-bf29-57da2d47a0b8",
   "metadata": {
    "tags": []
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'\\\\n'"
      ]
     },
     "execution_count": 60,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "R\"\\n\""
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 58,
   "id": "56d65308-9fb1-4709-84cc-0620907a1b02",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "\\n\n"
     ]
    }
   ],
   "source": [
    "print(R\"\\n\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 56,
   "id": "cef0c561-5c91-4c43-ab31-bf8e3455a92f",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "a\n",
      "\n"
     ]
    }
   ],
   "source": [
    "print(\"a\\n\")"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "ec60c1ee-5d75-42b5-be56-562eefc97721",
   "metadata": {},
   "source": [
    "#### Python 字符串格式化\n",
    "\n",
    "| 符  号 | 描述                                 |\n",
    "| :----- | :----------------------------------- |\n",
    "| %c     | 格式化字符及其ASCII码                |\n",
    "| %s     | 格式化字符串                         |\n",
    "| %d     | 格式化整数                           |\n",
    "| %u     | 格式化无符号整型                     |\n",
    "| %o     | 格式化无符号八进制数                 |\n",
    "| %x     | 格式化无符号十六进制数               |\n",
    "| %X     | 格式化无符号十六进制数（大写）       |\n",
    "| %f     | 格式化浮点数字，可指定小数点后的精度 |\n",
    "| %e     | 用科学计数法格式化浮点数             |\n",
    "| %E     | 作用同%e，用科学计数法格式化浮点数   |\n",
    "| %g     | %f和%e的简写                         |\n",
    "| %G     | %f 和 %E 的简写                      |\n",
    "| %p     | 用十六进制数格式化变量的地址 符  号  |"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "2f056487-0697-4ec1-b82d-ca30ca16af23",
   "metadata": {},
   "source": [
    "基于print使用"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 117,
   "id": "6ac61cb0-0db9-469d-bea9-684e708dc593",
   "metadata": {
    "tags": []
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "A\n"
     ]
    }
   ],
   "source": [
    "ch = 65   # ascll\n",
    "print(\"%c\" % ch)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 119,
   "id": "dc959c8d-d96c-4c63-a20a-66411a5108ca",
   "metadata": {
    "tags": []
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "data qwe\n"
     ]
    }
   ],
   "source": [
    "ch = \"data qwe\"   # ascll\n",
    "print(\"%s\" % ch)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 125,
   "id": "cb1fd932-b396-4fa3-9570-26a0d875994f",
   "metadata": {
    "tags": []
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "125\n"
     ]
    }
   ],
   "source": [
    "ch = 125.5\n",
    "print(\"%d\"%ch)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 147,
   "id": "41f0e686-4f71-4561-84cb-2be1041f81c5",
   "metadata": {
    "tags": []
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "3.141593\n",
      "3.14\n"
     ]
    }
   ],
   "source": [
    "ch = 3.1415926\n",
    "print(\"%f\" % ch)\n",
    "\n",
    "ch = 3.1415926\n",
    "print(\"%.2f\" % ch)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 149,
   "id": "caa50e7d-025a-45ae-ac92-1ca93255bc87",
   "metadata": {
    "tags": []
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "1.00e+07\n",
      "1.000000e+07\n"
     ]
    }
   ],
   "source": [
    "ch = 10000000\n",
    "print(\"%.2e\"%ch)\n",
    "\n",
    "ch = 10000000\n",
    "print(\"%e\"%ch)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "ab92458b-7c4e-4ed2-9bbb-ba37566f0b99",
   "metadata": {
    "tags": []
   },
   "source": [
    "*定义宽度或者小数点精度"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 170,
   "id": "0584bd0e-7e47-42e7-8e7e-0695762a3c79",
   "metadata": {
    "tags": []
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "     3.142\n"
     ]
    }
   ],
   "source": [
    "width = 10  # Replace 10 with the desired width\n",
    "precision = 3  # Replace 3 with the desired precision\n",
    "\n",
    "ch = 3.1415926\n",
    "print(\"%*.*f\" % (width, precision, ch))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "6e62d0d3-c63a-4fb6-a8f0-48de4426f933",
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": 166,
   "id": "f47bac2b-d467-4420-ab98-25a80e1dd665",
   "metadata": {
    "tags": []
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "   300.142\n"
     ]
    }
   ],
   "source": [
    "width = 10  # Replace 10 with the desired width\n",
    "precision = 3  # Replace 3 with the desired precision\n",
    "\n",
    "ch = 300.1415926\n",
    "print(\"{:{width}.{precision}f}\".format(ch, width=width, precision=precision))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "a42fc95e-6b4e-40dc-9644-e690e12ee982",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.11.5"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
